package com.css.fxfzypg.modules.baseData.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.repository.EmergencyShelterRepository;
import com.css.fxfzypg.modules.baseData.entity.EmergencyShelter;
import com.css.fxfzypg.modules.baseData.service.EmergencyShelterService;
import com.css.fxfzypg.modules.baseData.queryParams.EmergencyShelterQueryParams;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmergencyShelterServiceImpl implements EmergencyShelterService {
    @Resource
    EmergencyShelterRepository emergencyShelterRepository;

//    @Override
//    public GasstationDataVO updateGasstationData(GasstationDataVO gasstationDataVO) {
//
//        GasstationDataVO elevationVO1 = gasstationDataRepository.updateGasstationData(gasstationDataVO);
//
//        return elevationVO1;
//    }

    @Override
    @Transactional
    public EmergencyShelter saveEmergencyShelter(EmergencyShelter emergencyShelter) {
        emergencyShelter.setEsCreatetime(PlatformDateUtils.getCurrentTimestamp());
        emergencyShelter.setEsStatus(YNEnum.Y.toString());

        return emergencyShelterRepository.saveEmergencyShelter(emergencyShelter);
    }

    @Override
    public JSONObject getAllEmergencyShelter(EmergencyShelterQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(emergencyShelterRepository.getAllEmergencyShelterParams(queryParams));
    }

    @Override
    public EmergencyShelter getEmergencyShelterById(Integer id) {
        EmergencyShelter emergencyShelter = emergencyShelterRepository.getEmergencyShelterById(id);
        return emergencyShelter;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmergencyShelters(List<Integer> ids) {
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        emergencyShelterRepository.deleteEmergencySheltersById(ids,currentTimestamp);
    }

    @Override
    public List<EmergencyShelter> getAll() {
        return emergencyShelterRepository.getAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<EmergencyShelter> emergencyShelters = FileUtil.importExcel(file, EmergencyShelter.class);

        for(int i = 0 ; i<emergencyShelters.size();i++){
            emergencyShelters.get(i).setEsCreatetime(PlatformDateUtils.getCurrentTimestamp());
            emergencyShelters.get(i).setEsStatus(YNEnum.Y.toString());

        }
        emergencyShelterRepository.importExcel(emergencyShelters);
    }

    @Override
    public List<Integer> getEsMaxpeopleByInfluenceflyId(String influenceflyId) {
        List<EmergencyShelter> emergencyShelters = emergencyShelterRepository.getEmergencyShelter(influenceflyId);
        return emergencyShelters.stream().map(EmergencyShelter::getEsMaxpeople).collect(Collectors.toList());
    }
}
